package com.example.ddd.api;

import com.example.ddd.application.service.OrderApplicationService;
import com.example.ddd.domain.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器 - 表示层
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController {
    
    private final OrderApplicationService orderApplicationService;
    
    @Autowired
    public OrderController(OrderApplicationService orderApplicationService) {
        this.orderApplicationService = orderApplicationService;
    }
    
    // 创建订单
    @PostMapping
    public ResponseEntity<String> createOrder(@RequestParam String customerId) {
        String orderId = orderApplicationService.createOrder(customerId);
        return ResponseEntity.ok(orderId);
    }
    
    // 添加订单项
    @PostMapping("/{orderId}/items")
    public ResponseEntity<Void> addOrderItem(@PathVariable String orderId,
                                            @RequestParam String productId,
                                            @RequestParam int quantity,
                                            @RequestParam double price) {
        orderApplicationService.addOrderItem(orderId, productId, quantity, price);
        return ResponseEntity.noContent().build();
    }
    
    // 提交订单
    @PostMapping("/{orderId}/submit")
    public ResponseEntity<Void> submitOrder(@PathVariable String orderId) {
        orderApplicationService.submitOrder(orderId);
        return ResponseEntity.noContent().build();
    }
    
    // 支付订单
    @PostMapping("/{orderId}/pay")
    public ResponseEntity<Void> payOrder(@PathVariable String orderId) {
        orderApplicationService.payOrder(orderId);
        return ResponseEntity.noContent().build();
    }
    
    // 取消订单
    @PostMapping("/{orderId}/cancel")
    public ResponseEntity<Void> cancelOrder(@PathVariable String orderId) {
        orderApplicationService.cancelOrder(orderId);
        return ResponseEntity.noContent().build();
    }
    
    // 查询订单
    @GetMapping("/{orderId}")
    public ResponseEntity<Order> getOrder(@PathVariable String orderId) {
        Order order = orderApplicationService.getOrder(orderId);
        return ResponseEntity.ok(order);
    }
}